package com.lxmes.device.service.impl;

import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.device.domain.DeviceFailureCheck;
import com.lxmes.device.domain.DeviceFailureRepair;
import com.lxmes.device.domain.DeviceFailureReport;
import com.lxmes.device.mapper.DeviceFailureCheckMapper;
import com.lxmes.device.mapper.DeviceFailureRepairMapper;
import com.lxmes.device.mapper.DeviceFailureReportMapper;
import com.lxmes.device.service.IDeviceFailureCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备故障维修验证Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-04-26
 */
@Service
public class DeviceFailureCheckServiceImpl implements IDeviceFailureCheckService 
{
    @Autowired
    private DeviceFailureCheckMapper deviceFailureCheckMapper;
    @Autowired
    public DeviceFailureRepairMapper repairMapper;
    @Autowired
    public DeviceFailureReportMapper reportMapper;

    /**
     * 查询设备故障维修验证
     * 
     * @param id 设备故障维修验证主键
     * @return 设备故障维修验证
     */
    @Override
    public DeviceFailureCheck selectDeviceFailureCheckById(Long id)
    {
        return deviceFailureCheckMapper.selectDeviceFailureCheckById(id);
    }

    @Override
    public DeviceFailureCheck selectDeviceFailureCheckByReportId(String reportId) {
        return deviceFailureCheckMapper.selectDeviceFailureCheckByReportId(reportId);
    }

    /**
     * 查询设备故障维修验证列表
     * 
     * @param deviceFailureCheck 设备故障维修验证
     * @return 设备故障维修验证
     */
    @Override
    public List<DeviceFailureCheck> selectDeviceFailureCheckList(DeviceFailureCheck deviceFailureCheck)
    {
        return deviceFailureCheckMapper.selectDeviceFailureCheckList(deviceFailureCheck);
    }

    /**
     * 新增设备故障维修验证
     * 
     * @param deviceFailureCheck 设备故障维修验证
     * @return 结果
     */
    @Override
    public int insertDeviceFailureCheck(DeviceFailureCheck deviceFailureCheck)
    {
        deviceFailureCheck.setCreateTime(DateUtils.getNowDate());
        return deviceFailureCheckMapper.insertDeviceFailureCheck(deviceFailureCheck);
    }

    /**
     * 修改设备故障维修验证
     * 
     * @param deviceFailureCheck 设备故障维修验证
     * @return 结果
     */
    @Override
    public int updateDeviceFailureCheck(DeviceFailureCheck deviceFailureCheck)
    {
        deviceFailureCheck.setUpdateTime(DateUtils.getNowDate());
        return deviceFailureCheckMapper.updateDeviceFailureCheck(deviceFailureCheck);
    }

    @Override
    public int updateApiDeviceFailureCheck(DeviceFailureCheck deviceFailureCheck) {
        deviceFailureCheck.setUpdateTime(DateUtils.getNowDate());
        return deviceFailureCheckMapper.updateApiDeviceFailureCheck(deviceFailureCheck);
    }

    /**
     * 批量删除设备故障维修验证
     * 
     * @param ids 需要删除的设备故障维修验证主键
     * @return 结果
     */
    @Override
    public int deleteDeviceFailureCheckByIds(Long[] ids)
    {
        return deviceFailureCheckMapper.deleteDeviceFailureCheckByIds(ids);
    }

    /**
     * 删除设备故障维修验证信息
     * 
     * @param id 设备故障维修验证主键
     * @return 结果
     */
    @Override
    public int deleteDeviceFailureCheckById(Long id)
    {
        return deviceFailureCheckMapper.deleteDeviceFailureCheckById(id);
    }

    @Override
    public int apiDoCheck(DeviceFailureCheck deviceFailureCheck) {
        if(StringUtils.isNotNull(deviceFailureCheck)){
            deviceFailureCheck.setCreateBy(SecurityUtils.getUsername());
            String reportId=  deviceFailureCheck.getReportId();
            DeviceFailureCheck check = deviceFailureCheckMapper.selectDeviceFailureCheckByReportId(reportId);
            DeviceFailureRepair repair = repairMapper.selectDeviceFailureRepairByReportId(reportId);
            DeviceFailureReport report = reportMapper.selectDeviceFailureReportByReportId(reportId);
        if(StringUtils.isNotNull(check)){
             deviceFailureCheckMapper.updateApiDeviceFailureCheck(deviceFailureCheck);
        }else {
              deviceFailureCheckMapper.insertDeviceFailureCheck(deviceFailureCheck);
          }
         //验证状态 5 --驳回 4--完成状态
            repair.setFaultProgress(deviceFailureCheck.getIsFinish());
            report.setFaultProgress(deviceFailureCheck.getIsFinish());
          /*  String isFinish = deviceFailureCheck.getIsFinish();
          if(StringUtils.equals("0",isFinish)){
              repair.setFaultProgress("2");
              report.setFaultProgress("2");
          }else {
              repair.setFaultProgress("4");
              report.setFaultProgress("4");
          }*/
            repairMapper.updateDeviceFailureRepair(repair);
            reportMapper.updateDeviceFailureReport(report);
            return 1;
        }
        return 0;
    }
}
